//
// Created by none98 on 2021/9/10.
//

#ifndef LOGINSERVER_CHANNEL_H
#define LOGINSERVER_CHANNEL_H

#include <sys/epoll.h>
#include <functional>
#include <memory>

class Selector;
class EventLoop;

class Channel {
    friend class Selector;
public:
    typedef std::function<void()> EventCallBack;
    Channel(int fd, unsigned int interestedEvnets):
        fd(fd),
        activeEvents(0),
        interestedEvents(interestedEvnets)
    {

    }

    ~Channel()=default;

    inline void SetEvent(unsigned int event){
        interestedEvents|=event;
    }

    inline void CancelEvent(unsigned int event){
        unsigned int reverseNum = ~event;
        interestedEvents&=reverseNum;
    }

    void SetReadCallBack(const EventCallBack &call){
            readCallBack=call;
    }

    void SetWriteCallBack(const EventCallBack &call){
        writeCallBack=call;
    }

    void SetErrorCallBack(const EventCallBack &call){
        errorCallBack=call;
    }

    unsigned int GetInterestedEvents(){
        return interestedEvents;
    }

    void HandleEvents();


    inline void SetFd(int fd){
        this->fd=fd;
    }

private:
    unsigned int activeEvents;
    unsigned int interestedEvents;
    int fd;
    EventCallBack readCallBack;
    EventCallBack writeCallBack;
    EventCallBack errorCallBack;

    inline int GetFd(){
        return fd;
    }

};


#endif //LOGINSERVER_CHANNEL_H
